﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using uREST.Core.Handlers;
using uREST.Core.Attributes;
using uREST.CMS.Extensions;
using OpenRasta.Web;
using umbraco.cms.businesslogic.web;
using System.Runtime.InteropServices;
using uREST.Blog.Extensions;
using uREST.CMS.Util;

namespace uREST.Blog.Handlers
{
    [UmbracoAuthorize(Applications = "content")]
    public class PostHandler : AbstractHandler
    {
        public const string DOC_TYPE_ALIAS = "BlogPost";

        public PostHandler(ICommunicationContext context)
            : base(context)
        { }

        // GET (LIST)
        [HttpOperation(ForUriName = "PostList")]
        public OperationResult GetPostList(
            [Optional] int blogId)
        {
            var docs = new List<Document>();

            if (blogId > 0)
            {
                Document blogDoc = null;

                try
                {
                    blogDoc = new Document(blogId);
                }
                catch
                { }

                if (blogDoc == null)
                    return new OperationResult.NotFound();

                if (!AuthenticatedUser.CanAccess(blogDoc))
                    return new OperationResult.Forbidden();

                var blogDocType = DocumentType.GetByAlias(BlogHandler.DOC_TYPE_ALIAS);
                if (blogDocType == null || blogDocType.Alias != blogDoc.ContentType.Alias)
                    return new OperationResult.NotFound();

                var postDocType = DocumentType.GetByAlias(PostHandler.DOC_TYPE_ALIAS);
                if (postDocType == null)
                    return new OperationResult.NotFound();

                docs.AddRange(UmbracoHelper.GetDocumentsByDocTypeAlias(PostHandler.DOC_TYPE_ALIAS, blogDoc.Id, AuthenticatedUser));
            }
            else
            {
                docs.AddRange(UmbracoHelper.GetDocumentsByDocTypeAlias(PostHandler.DOC_TYPE_ALIAS, AuthenticatedUser));
            }
            
            return new OperationResult.OK { ResponseResource = docs.ToPostResourceList() };
        }

        // GET (SINGLE)
        [HttpOperation(ForUriName = "Post")]
        public OperationResult GetPost(
            int id)
        {
            Document doc = null;

            try
            {
                doc = new Document(id);
            }
            catch
            { }

            if (doc == null)
                return new OperationResult.NotFound();

            if (!AuthenticatedUser.CanAccess(doc))
                return new OperationResult.Forbidden();

            var blogDocType = DocumentType.GetByAlias(PostHandler.DOC_TYPE_ALIAS);
            if (blogDocType == null || blogDocType.Alias != doc.ContentType.Alias)
                return new OperationResult.NotFound();

            return new OperationResult.OK { ResponseResource = doc.ToPostResource() };
        }
    }
}
